using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class forums_controls_administration_AddForum : System.Web.UI.UserControl
{
    public event EventHandler ForumCreated;

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            DAL.Forum myForum = new DAL.Forum();
            myForum.Name = txtName.Text;
            myForum.Description = txtDescription.Text;
            myForum.Parent = ddlRoot.CurrentForum;

            DAL.Forums.Add(myForum);

            lblSuccess.Visible = true;
            lblSuccess.Text = GetSuccessMessage(myForum);
            
            Refresh();
            ClearForm();

            if (ForumCreated != null)
            {
                ForumCreated(this, new EventArgs());
            }
        }
    }

    private string GetSuccessMessage(DAL.Forum newForum)
    {
        return string.Format("<p><span style=\"color : #ff0000;\"><b>{0}</b> was successfully created.</span></p>",
            Server.HtmlEncode(newForum.Name));
    }

    public void Refresh()
    {
        ddlRoot.Refresh();
    }

    private void ClearForm()
    {
        txtName.Text = "";
        txtDescription.Text = "";
        ddlVisibility.SelectedIndex = 0;
    }
}
